%The code estimates the portfolio CVaR and the asset weights 
%该代码估计了投资组合CVaR和资产权重
%INPUTS:
%输入内容
%The data matrix (historical returns or simulation) ScenRets size JxnAssets
%数据矩阵（历史回报或模拟）场景大小为JXNASSET
% the confidence level beta (scalar, between 0.9 and 0.999, usually o.95 or
% 0.99)
%置信度beta
%the Upper Bounds for the weights in order to inforce diversification 
%权重的上界
%R0 the portfolio target return
%R0 组合目标返回值
[J, nAssets]=size(ScenRets);
i=1:nAssets;
beta=0.99; %change it if you want but stay between 0.9 and 0.999 可以改变但是需要使得beta在 0.9 和 0.999之间
LB=-0.1;
UB=0.5;  %the upper bound to inforce diversification, positive between (0,1)%权重的上界
R0=0;  %the target return%返回值
ShortP=0; %If ShortP=1 allow for short positions, else if ShortP=0 only long positions are allowed
%如果该值为1时允许短的位置，如果为0时只允许长的位置
%function to be minimized
%待最小化函数
%w(31)=VaR
objfun=@(w) w(nAssets+1)+(1/J)*(1/(1-beta))*sum(max(-w(i)*ScenRets(:,i)'-w(nAssets+1),0));
% initial guess
%初始的猜测值
w0=[(1/nAssets)*ones(1,nAssets)];
VaR0=abs(quantile(ScenRets*w0',1-beta)); % the initial guess for VaR is the
%HS VaR of the equally weighted portfolio
%VaR的初值是等权投资组合的VaR
w0=[w0 VaR0];
% the (linear) equalities and unequalities matrixes
%（线性）等式与不等式矩阵
A=[-mean(ScenRets) 0];
if ShortP==0
A=[A;  -eye(nAssets) zeros(nAssets,1)];
A=[A; eye(nAssets) zeros(nAssets,1)];
b=[-R0 zeros(1,nAssets) UB*ones(1,nAssets)];
elseif ShortP==1
A=[A;  -eye(nAssets) zeros(nAssets,1)];
A=[A; eye(nAssets) zeros(nAssets,1)];
b=[-R0 -LB*ones(1,nAssets) UB*ones(1,nAssets)];
elseif ShortP~=0|ShortP~=1
    error('Input ShortP=1 (line14) if you allow short positions and 0 else!!')
end
b=b';
Aeq=[ ones(1,nAssets) 0];
beq=[1];
options=optimset('LargeScale','off');
options=optimset(options,'MaxFunEvals',10000);
[w,fval,exitflag,output]=fmincon(objfun,w0,A,b,Aeq,beq,[],[],[],options)
portfWeights=zeros(1, nAssets);
for i=1:nAssets
% clear rounding errors
%清空舍入误差
    if w(i)<0.0001
        w(i)=0;
    end
% save results to the workfile
%保存结果
    portfWeights(1,i)=w(i);
end
Risk=zeros(1,2)
Risk(1,1)=w(nAssets+1)%Remember that w(31)= portfolio VaR
Risk(1,2)=fval
